package cn.com.fuhuitong.main.mine.ui.activity

import androidx.lifecycle.Observer
import cn.com.fuhuitong.aroute.ARouteConstance
import cn.com.fuhuitong.main.base.ViewModeActivity
import com.alibaba.android.arouter.facade.annotation.Route
import cn.com.fuhuitong.R
import cn.com.fuhuitong.base.EmptyContent
import cn.com.fuhuitong.entity.EmptyState
import cn.com.fuhuitong.http.HttpResponseStatus
import cn.com.fuhuitong.main.ItemViewType
import cn.com.fuhuitong.main.mine.adapter.WithdrawListAdapter
import cn.com.fuhuitong.main.mine.vm.WithdrawViewModel
import com.alibaba.android.vlayout.layout.LinearLayoutHelper
import kotlinx.android.synthetic.main.activity_mine_withdraw_list.*

@Route(path = ARouteConstance.ACTIVITY_URL_MINE_WITHDRAW_LIST)
class MineWithdrawListActivity : ViewModeActivity<WithdrawViewModel>() {

    private lateinit var withdrawListAdapter: WithdrawListAdapter

    private val mType: Int
        get() = intent.getIntExtra("actionType", 1)

    override fun viewModel(): WithdrawViewModel = createViewModel()

    override val layoutResId: Int
        get() = R.layout.activity_mine_withdraw_list

    override fun initView() {

        image_withdraw_list_back.setOnClickListener {
            this.finish()
        }

        this.initViewRecyclerAdapter(
            recycler_withdraw_list, arrayOf(ItemViewType.WITHDRAW_LIST)
        )

        delegateAdapter.addAdapter(
            WithdrawListAdapter(this, LinearLayoutHelper())
                .apply { withdrawListAdapter = this }
        )

    }

    override fun initViewMode() {
        viewModel.withdrawListDataLiveData.observe(this, Observer { response ->
            handlerResponseEmpty(
                status = response.status, message = response.statusTip,
                length = response.response?.data?.size ?: 0,
                emptyContent = EmptyContent("去提现", "暂无数据")
            )
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            response.response?.data?.let {
                if (it.size == 0)
                    return@let
                withdrawListAdapter.setAddDataNew(it)
            }
        })
    }

    override fun initData() {
        if (mType == 1) {
            viewModel.cashStoreList()
        } else if (mType == 2) {
            viewModel.cashList()
        }
    }

    override fun onLoadingEmptyView(state: EmptyState) {
        super.onLoadingEmptyView(state)
        if (state == EmptyState.FAILURE) {
            initData()
        } else if (state == EmptyState.NULL) {
            this.finish()
        }
    }
}